﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using System.ComponentModel.Composition;
using EIA.Basic.Model;

namespace EIA.SLClient.PlatManage.BasicMgr
{
    public class PositionListViewModel : SimpleMvvmToolkit.ViewModelBase<PositionListViewModel>
    {
        public PositionListViewModel()
        {
            this.RegisterToReceiveMessages(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.POSITION_RETRIEVE, EP_Retrieve);
            this.RegisterToReceiveMessages<TPosition>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.POSITION_NEW_CANCEL, EP_NewCancel);
        }

        private void EP_Retrieve(object sender, SimpleMvvmToolkit.NotificationEventArgs e)
        {
            RetrieveItems();
        }
        private void EP_NewCancel(object sender, SimpleMvvmToolkit.NotificationEventArgs<TPosition> e)
        {
            if (e.Data != null && TPositionCollection != null) TPositionCollection.Remove(e.Data);
        }

        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.POSITION_BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.POSITION_BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected PagedCollectionView _TPositionCollection;
        public PagedCollectionView TPositionCollection
        {
            get { return _TPositionCollection; }
            set
            {
                _TPositionCollection = value;
                NotifyPropertyChanged(m => m.TPositionCollection);
            }
        }

        #endregion

        #region command

        #region RetrieveItemsCommand

        private SimpleMvvmToolkit.DelegateCommand _RetrieveItemsCommand = null;
        public SimpleMvvmToolkit.DelegateCommand RetrieveItemsCommand
        {
            get
            {
                if (_RetrieveItemsCommand == null)
                {
                    _RetrieveItemsCommand = new SimpleMvvmToolkit.DelegateCommand(RetrieveItems, CanExcuteRetrieveItems);
                } return _RetrieveItemsCommand;
            }
            set { _RetrieveItemsCommand = value; NotifyPropertyChanged(m => m.RetrieveItemsCommand); }
        }

        public void RetrieveItems()
        {
            EIA.Basic.BLL.ClientProxy.TPositionProxy proxy = new EIA.Basic.BLL.ClientProxy.TPositionProxy();
            Busy();
            proxy.ListAll((lst) =>
            {
                Free();
                TPositionCollection = new PagedCollectionView(lst);
            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("获取职位列表发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteRetrieveItems()
        {
            return true;
        }

        #endregion

        #region InsertItemCommand

        private SimpleMvvmToolkit.DelegateCommand _InsertItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand InsertItemCommand
        {
            get
            {
                if (_InsertItemCommand == null)
                {
                    _InsertItemCommand = new SimpleMvvmToolkit.DelegateCommand(InsertItem, CanExcuteInsertItem);
                } return _InsertItemCommand;
            }
            set { _InsertItemCommand = value; NotifyPropertyChanged(m => m.InsertItemCommand); }
        }

        public void InsertItem()
        {
            TPosition pos = new TPosition();
            pos.PosName = "新职位";
            pos.PosLevel = 0;

            List<TPosition> lst = new List<TPosition>();
            lst.Add(pos);
            lst.AddRange(TPositionCollection.SourceCollection as IList<TPosition>);
            TPositionCollection = new PagedCollectionView(lst);

            this.SendMessage<TPosition>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.POSITION_NEW, new SimpleMvvmToolkit.NotificationEventArgs<TPosition>("", pos));
        }

        public bool CanExcuteInsertItem()
        {
            return true;
        }

        #endregion

        #region UpdateItemCommand

        private SimpleMvvmToolkit.DelegateCommand<EIA.Basic.Model.TPosition> _UpdateItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<EIA.Basic.Model.TPosition> UpdateItemCommand
        {
            get
            {
                if (_UpdateItemCommand == null)
                {
                    _UpdateItemCommand = new SimpleMvvmToolkit.DelegateCommand<EIA.Basic.Model.TPosition>(UpdateItem, CanExcuteUpdateItem);
                } return _UpdateItemCommand;
            }
            set { _UpdateItemCommand = value; NotifyPropertyChanged(m => m.UpdateItemCommand); }
        }

        public void UpdateItem(EIA.Basic.Model.TPosition p)
        {
            this.SendMessage<TPosition>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.POSITION_UPDATE, new SimpleMvvmToolkit.NotificationEventArgs<TPosition>("", p));
        }

        public bool CanExcuteUpdateItem(EIA.Basic.Model.TPosition p)
        {
            return true;
        }

        #endregion

        #region DeleteItemCommand

        private SimpleMvvmToolkit.DelegateCommand<EIA.Basic.Model.TPosition> _DeleteItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<EIA.Basic.Model.TPosition> DeleteItemCommand
        {
            get
            {
                if (_DeleteItemCommand == null)
                {
                    _DeleteItemCommand = new SimpleMvvmToolkit.DelegateCommand<EIA.Basic.Model.TPosition>(DeleteItem, CanExcuteDeleteItem);
                } return _DeleteItemCommand;
            }
            set { _DeleteItemCommand = value; NotifyPropertyChanged(m => m.DeleteItemCommand); }
        }

        public void DeleteItem(EIA.Basic.Model.TPosition p)
        {
            if (System.Windows.MessageBox.Show(string.Format("确认删除职位 [{0}]", p.PosName), "提示", System.Windows.MessageBoxButton.OKCancel) != System.Windows.MessageBoxResult.OK) return;

            EIA.Basic.BLL.ClientProxy.TPositionProxy proxy = new Basic.BLL.ClientProxy.TPositionProxy();
            Busy();
            proxy.Delete(p.PosId, (r) =>
            {

                Free();
                if (r)
                {
                    TPositionCollection.Remove(p);
                }
                else
                {
                    System.Windows.MessageBox.Show("删除职位失败！", "提示", System.Windows.MessageBoxButton.OK);
                }

            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("删除职位发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteDeleteItem(EIA.Basic.Model.TPosition p)
        {
            return true;
        }

        #endregion

        #endregion

        public void OnLoad()
        {
            if (TPositionCollection == null) RetrieveItems();
        }
    }
}